#include <algorithm>
#include <iostream>
#include <vector>
#define LL long long
using namespace std;

const int kInf = 1e9;

int main() {
  freopen("paint.in", "r", stdin);
  freopen("paint.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> dp(m, vector<int>(m - 1, kInf)), g;
  vector<int> c(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }

  int ans = kInf;

  g = dp;
  for (int i = 1; i <= n; i++) {
    fill(dp[i % m].begin(), dp[i % m].end(), kInf);
    for (int j = 0; j < m - 1 && j < i; j++) {
      if (i <= m) {
        dp[i % m][j] = c[i] + c[i - j - 1];
      } else {
        dp[i % m][j] = min(dp[i % m][j], g[(i - j - 1) % m][m - j - 2] + c[i]);
      }
      if (i - j - 1 >= n - m + 1) {
        ans = min(ans, dp[i % m][j]);
      }
    }
    g[i % m] = dp[i % m];
    for (int j = 1; j < m - 1; j++) {
      g[i % m][j] = min(g[i % m][j], g[i % m][j - 1]);
    }
  }
  cout << ans << "\n";
  return 0;
}